package com.xiaopeng.system;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.builder.CustomFile;
import com.baomidou.mybatisplus.generator.fill.Column;

import java.util.Collections;

/**
 * 代码生成器
 */
public class CodeGenerator {


    public static void main(String[] args) {

        String author = "xiaopeng"; // 作者
        String moduleName = "basic";
        String[] tableList = new String[]{ "operation_log" };
        String projectPath = System.getProperty("user.dir"); // 拿到工程的绝对路径


        // 数据源配置
        DataSourceConfig dataSourceConfig = new DataSourceConfig.Builder(
                "jdbc:mysql://localhost:3306/xiaopeng?&serverTimezone=Asia/Shanghai", "root", "123456").build();



        // 全局配置
        GlobalConfig globalConfig = new GlobalConfig.Builder()
                .author(author)
                .outputDir(projectPath + "/src/main/java")  // 输出路径
                .disableOpenDir()   // 禁用打开目录
                .enableSwagger()
                .build();


        // 包配置
        PackageConfig packageConfig = new PackageConfig.Builder()
                .parent("com.xiaopeng.system.basic") // 设置父包名，默认值:com.baomidou
                .moduleName(moduleName) // 设置父包模块名，默认值:无
                .entity("entity")// Entity 包名，默认值:entity
                .mapper("mapper")// Mapper 包名，默认值:mapper
                .xml("mapper.xml")// Mapper XML 包名，默认值:mapper.xml
                .service("service")// Service 包名，默认值:service
                .serviceImpl("service.impl")// Service Impl 包名，默认值:service.impl
                .controller("controller")// Controller 包名，默认值:controller
                .pathInfo(Collections.singletonMap(OutputFile.xml, projectPath + "/src/main/resources/mapper")) // 路径配置信息
                .build();



        // 策略配置
        StrategyConfig strategyConfig = new StrategyConfig.Builder()
                .addInclude(tableList)
                // Entity配置
                .entityBuilder()
                .formatFileName("%sEntity")
                .enableLombok()// 开启 lombok 模型
                .logicDeleteColumnName("is_deleted")// 逻辑删除字段名(数据库)
                .logicDeletePropertyName("isDeleted")// 辑删除属性名(实体)
                .addTableFills(new Column("update_time", FieldFill.INSERT_UPDATE))// 添加表字段填充 TODO 用不到则注释掉
                // Mapper配置
                .mapperBuilder()
                .superClass(BaseMapper.class)   // 设置父类
                .build();


        // 自定义配置
        CustomFile customFile = new CustomFile.Builder()
                .fileName("%sEntity")
                .enableFileOverride()
                .build();
        InjectionConfig injectionConfig = new InjectionConfig.Builder().customFile(customFile).build();



        // 代码生成器
        AutoGenerator autoGenerator = new AutoGenerator(dataSourceConfig);
        autoGenerator.global(globalConfig);
        autoGenerator.packageInfo(packageConfig);
        autoGenerator.strategy(strategyConfig);
//        autoGenerator.injection(injectionConfig);
        // 执行
        autoGenerator.execute();
    }




}
